-- David Siegel, Jonathan Coveney, HW #8

module Term (Term (..), TruthAssignment, KnowledgeBase,
             showTa
            ) where

import List

------------------------------------------------------------
-- Public types
------------------------------------------------------------

type LiteralName = Char

-- A mapping from LiteralNames to truth values.
type TruthAssignment = [(Term, Bool)]

-- A knowledgebase is a "set" of logic sentences.
type KnowledgeBase = [Term]

-- Term type represents sentences in propositional logic.
data Term =   And Term Term
            | Or Term Term
            | If Term Term
            | Iff Term Term
            | Not Term
            | Literal LiteralName
            | TrueValue
            | FalseValue
 deriving (Eq)

instance Show Term where
  show (a `And` b) = "(" ++ show a ++ " & " ++ show b ++ ")"
  show (a `Or` b) = "(" ++ show a ++ " | " ++ show b ++ ")"
  show (a `If` b) = "(" ++ show a ++ " => " ++ show b ++ ")"
  show (a `Iff` b) = "(" ++ show a ++ ") <=> (" ++ show b ++ ")"
  show (Not (Literal a)) = '~' : [a]
  show (Not a) = "~(" ++ show a ++ ")"
  show (Literal a) = [a]
  show (TrueValue) = "True"
  show (FalseValue) = "False"

------------------------------------------------------------
-- Public method declarations
------------------------------------------------------------

showTa :: TruthAssignment -> String

------------------------------------------------------------
-- Public methoid definitions
------------------------------------------------------------

showTa ta = let ta' = map (\(Literal a, val) -> (a, val)) ta
                pretty (a, val) = a : ": " ++ show val ++ "\n" in
                concatMap pretty (sort ta')


